package com.m3958.firstgwt.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.m3958.firstgwt.client.AppStatus;
import com.m3958.firstgwt.client.Ieditor;
import com.m3958.firstgwt.client.IhasLazyRichEditor;
import com.m3958.firstgwt.client.IwantLoadCssScripts;
import com.m3958.firstgwt.client.ScriptCssLoader;



public class KindeditorArea extends Composite implements IwantLoadCssScripts,Ieditor {

	private static KindeditorAreaUiBinder uiBinder = GWT
			.create(KindeditorAreaUiBinder.class);

	interface KindeditorAreaUiBinder extends UiBinder<Widget, KindeditorArea> {
	}
	
	@UiField(provided=true)
	TextArea keContainer;
	
	@UiField
	HTMLPanel wrapDiv;
	
	private ScriptCssLoader scriptLoader;
	
	private String kindEditBasePath = "/ke402/";
	
	private String ker =  kindEditBasePath + "kindeditor-min.js";
	private String kerLang = kindEditBasePath + "lang/zh_CN.js";
	
	private String editorId;
	
	private boolean initialized = false;
	
	private IhasLazyRichEditor ec;
	
	public boolean isInitialized() {
		return initialized;
	}

	@Inject
	private AppStatus appStatus;
	
	@Inject
	public KindeditorArea(ScriptCssLoader scriptLoader,@Assisted("editorId") String editorId,@Assisted("fieldName") String fieldName,@Assisted("width") String width,@Assisted("height") String height) {
		this.scriptLoader = scriptLoader;
		this.editorId = editorId;
		initEditor(fieldName,width,height);
	}
	
	public void setContainer(IhasLazyRichEditor ec){
		this.ec = ec;
	}

	public void initEditor(String fieldName,String width,String height) {
		keContainer = new TextArea();
		keContainer.setWidth(width);
		keContainer.setHeight(height);
		keContainer.setName(fieldName);
		DOM.setElementAttribute(keContainer.getElement(), "id", editorId);
		initWidget(uiBinder.createAndBindUi(this));
		wrapDiv.setWidth(width);
		JsArrayString scripts = JsArrayString.createArray().cast();
		scripts.push(ker);
		scripts.push(kerLang);
		scriptLoader.loadScriptsViaCallback(scriptLoader, scripts,"js","加载kindeditor脚本", this);
		
	}

	@Override
	public void onScriptLoadSuccess(String name) {
		createEditor(appStatus.getSiteId(),editorId,kindEditBasePath);
		this.initialized = true;
		this.ec.editorInitliazed();
	}

	@Override
	public void onScriptLoadFailure(String name) {
		Window.alert("编辑器加载失败！请刷新浏览器！");
	}
	
	 public final native void createEditor(String siteId,String editorId,String kindEditorBasePath)/*-{
	 	if(!$wnd.m3958editors){
	 		$wnd.m3958editors = {};
	 	}
	     var options = {
	     	uploadJson:"/fileUploadHandler/?siteId=" + siteId + "&WANTED_RESPONSE=KINDEDITORJSON",
	     	allowFileManager:false,
	     	basePath:kindEditorBasePath
			};
	     $wnd.m3958editors[editorId] = $wnd.KindEditor.create('#' + editorId,options);
	 }-*/;
	 
	 private final native void setHtml(KindeditorArea ka,String html)/*-{
	 	var editor = $wnd.m3958editors[ka.@com.m3958.firstgwt.client.ui.KindeditorArea::editorId];
		editor.html(html);
		editor.fullscreen();
		editor.fullscreen();
	 }-*/;
	 
	 private final native String getHtml(KindeditorArea ka)/*-{
	 	var editor = $wnd.m3958editors[ka.@com.m3958.firstgwt.client.ui.KindeditorArea::editorId];
	 	editor.sync();
		return editor.html();
	 }-*/;

	@Override
	public void setHtml(String html) {
		setHtml(this, html);
		
	}

	@Override
	public String getHtml() {
		return getHtml(this);
	}
	 
}
